package com.ztmg.water.service.system.impl;

import com.ztmg.water.model.system.SysDist;
import com.ztmg.water.vo.system.SysDistVO;
import com.ztmg.water.dto.system.SysDistDTO;
import com.ztmg.water.dto.system.SysDistPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.system.SysDistService;
import com.ztmg.water.mapper.system.SysDistMapper;import com.ztmg.water.model.system.SysDistExample;

@Service
public class SysDistServiceImpl implements SysDistService{

    @Resource
    private SysDistMapper sysDistMapper;
    @Override
    public JsonResult create(SysDistDTO dto) {
        if(null != dto){
            SysDist bean = new SysDist();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            sysDistMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(SysDistDTO dto) {
        if(null != dto && null != dto.getId()){
            SysDist bean = new SysDist();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            sysDistMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<SysDistVO> selectById(Integer id) {
        if(null != id){
            SysDist bean = sysDistMapper.selectByPrimaryKey(id);
            SysDistVO vo = new SysDistVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<SysDistVO> select(SysDistDTO dto) {
        SysDistExample example = new SysDistExample();
        SysDistExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<SysDist> sysUserList = sysDistMapper.selectByExample(example);
        SysDistVO vo = new SysDistVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<SysDistVO>> selectPage(SysDistPageDTO dto) {
        SysDistExample example = new SysDistExample();
        SysDistExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<SysDist> beanList = sysDistMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<SysDistVO> respDTOS = beanList.parallelStream().map((bean) -> {
            SysDistVO vo = new SysDistVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( sysDistMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(SysDistDTO dto) {
        SysDistExample example = new SysDistExample();
        SysDistExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(sysDistMapper.deleteByExample(example));
    }
}